package com.mtiiot.fsmanage.controller;

import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.inter1.RedisService1;
import com.mtiiot.fsmanage.bean.parameter.Sys;
import com.mtiiot.fsmanage.server.CommonServer;
import com.mtiiot.fsmanage.utils.FsViewConfig;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 公共部分
 */
@SuppressWarnings("all")
@Controller
public class CommonController {
	private static Logger logger = LogManager.getLogger(CommonController.class);
    @Autowired
    private CommonServer commonServer;

    @Autowired
    private FsViewConfig fsViewConfig;

    @Autowired
    private RedisService1 redisService1;
    /**
     * 右边页面跳转
     * @param
     * @return
     */
    @RequestMapping("changePage.action")
    public Object changePage(String code,HttpServletRequest r)throws Exception{
        r.setAttribute("code",code);
        return "page/"+code+"/"+code;
    }

    /**
     * 右边页面页签跳转
     * @param
     * @return
     */
    @RequestMapping("changeSheet.action")
    public Object changeSheet(String code,String sheet,HttpServletRequest r){
        r.setAttribute("code",code);
        r.setAttribute("sheet",sheet);
        return "page/"+code+"/"+code+"_"+sheet;
    }

    /**
     * 登出
     * @param
     * @return
     */
    @RequestMapping(value="loginOut.action",produces="application/text")
    @ResponseBody
    public Object loginOut(HttpServletRequest r){
        r.getSession().setAttribute("user", null);
        return "success";
    }

    /**
     * 系统树获取根节点节点
     * @return
     */
    @RequestMapping(value="getRootNode.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object getRootNode() throws Exception {
        Object obj=commonServer.getRootNode();
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr=mapper.writeValueAsString(obj);
        return jsonStr;
    }

    /**
     * 系统树获取根节点节点
     * @return
     */
    @RequestMapping(value="getChildrenNode.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object getChildrenNode(String id,String sysName,String l1Name,String l2Name ,String type,String fileType) throws Exception{
        Object obj = commonServer.getChildrenNode(id,sysName,l1Name,l2Name,type,fileType);
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr=mapper.writeValueAsString(obj);
        return jsonStr;
    }

    /**
     * 系统树获取根节点节点
     * @return
     */
    @RequestMapping(value="getSysInfo.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object getSysInfo() throws Exception{
        String sysId=fsViewConfig.getFsviewSysId();
        String sysRedisKey=fsViewConfig.getSysRedisKey();
        Map<String,String> obj=redisService1.hgetAll(new MetadataInfo(sysId),sysRedisKey);
        List<Sys> lists=new ArrayList<Sys>();
        Set<String> sets=obj.keySet();
        Sys sys=null;
        for(String key:sets){
            sys=new Sys();
            sys.setId(key);
            sys.setSysName(obj.get(key));
            lists.add(sys);
        }
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr=mapper.writeValueAsString(lists);
        return jsonStr;
    }
};